package com.evil.common.application.enums;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum ApplicationTypeEnum {

    /**
     * 应用类型
     */
    FORM_PROCESS_APP(1, "表单流程应用"),
    FORM_APP(2, "表单应用"),
    VIEW_APP(3, "视图应用"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<ApplicationTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static ApplicationTypeEnum isExistById(Integer id) {
        return ApplicationTypeEnum.findById(id).orElseThrow(ApplicationTypeEnum::getBusinessException);
    }

    public static boolean needProcess(Integer id) {
        return null != id && FORM_PROCESS_APP.id == id;
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "应用类型参数错误");
    }
}
